#include "BaseTile.h"
#include "Assets/TextureManager.h"
#include "Helpers/Filepath.h"

bool BaseTile::m_bTexturesLoaded=false;

BaseTile::BaseTile(TileType type, TileColor color, ivec2 position)
	:star::Object()
	,m_Type(type)
	,m_TileColor(color)
	,m_Position(position)
	,m_SpriteTile(nullptr)
	,m_SwitchTime(2.0f)
	,m_ElpasedTime(0)
	,m_bInitialized(false)
	,m_SquareID(0)
{
	if(!m_bTexturesLoaded)
	{
		m_bTexturesLoaded=true;
		LoadAllTextures();

	}
}

void BaseTile::Initialize()
{
	m_SpriteTile = new star::SpritesheetComponent(_T("Tiles/Tiles.png"),_T("Tiles"),_T("Tiles"));
	//SetColorType(m_Type,m_TileColor);
	AddComponent(m_SpriteTile);
	SetPosition(m_Position);

}


void BaseTile::SetType( TileType type )
{
	m_Type = type;
	SetTypeColor(m_Type,m_TileColor);
}

void BaseTile::SetColor( TileColor color )
{
	m_TileColor=color;
	SetTypeColor(m_Type,m_TileColor);
}

glm::vec2 BaseTile::GetPosition()
{
	return GetTransform()->GetWorldPosition().pos2D();
}

void BaseTile::SetPosition( ivec2 position )
{
	GetTransform()->Translate(static_cast<float>(position.x),static_cast<float>(position.y));
}

void BaseTile::SetSwitchTime( float time )
{
	m_SwitchTime=time;
}

void BaseTile::LoadAllTextures()
{
	//star::Filepath path(_T("Tiles/"),_T("Tiles.png"));
	//star::TextureManager::GetInstance()->LoadTexture(path.GetFullPath(),_T("Tiles"));
}

void BaseTile::SetTypeColor( TileType type,TileColor color )
{
	int offset =0;

	//if color is set to black then we call the white to black animation : ex Left_White
	switch (color)
	{
	case BLACKTILE:

		switch (type)
		{
		case LEFT:
			m_SpriteTile->PlayAnimation(_T("Left_White"));
			break;

		case TOP:
			m_SpriteTile->PlayAnimation(_T("Top_White"));
			break;

		case RIGHT:
			m_SpriteTile->PlayAnimation(_T("Right_White"));
			break;

		case BOTTOM:
			m_SpriteTile->PlayAnimation(_T("Bottom_White"));
			break;

		case TOPLEFT:
			m_SpriteTile->PlayAnimation(_T("TopLeft_White"));
			break;

		case TOPRIGHT:
			m_SpriteTile->PlayAnimation(_T("TopRight_White"));
			break;

		case BOTTOMLEFT:
			m_SpriteTile->PlayAnimation(_T("BottomLeft_White"));
			break;

		case BOTTOMRIGHT:
			m_SpriteTile->PlayAnimation(_T("BottomRight_White"));
			break;

		case NOBORDER:
			m_SpriteTile->PlayAnimation(_T("NoBorder_White"));
			break;

		case NORMAL:
			m_SpriteTile->PlayAnimation(_T("Normal_White"));
			break;
		}

		break;

	case WHITETILE:

		switch (type)
		{
		case LEFT:
			m_SpriteTile->PlayAnimation(_T("Left_Black"));
			break;

		case TOP:
			m_SpriteTile->PlayAnimation(_T("Top_Black"));
			break;

		case RIGHT:
			m_SpriteTile->PlayAnimation(_T("Right_Black"));
			break;

		case BOTTOM:
			m_SpriteTile->PlayAnimation(_T("Bottom_Black"));
			break;

		case TOPLEFT:
			m_SpriteTile->PlayAnimation(_T("TopLeft_Black"));
			break;

		case TOPRIGHT:
			m_SpriteTile->PlayAnimation(_T("TopRight_Black"));
			break;

		case BOTTOMLEFT:
			m_SpriteTile->PlayAnimation(_T("BottomLeft_Black"));
			break;

		case BOTTOMRIGHT:
			m_SpriteTile->PlayAnimation(_T("BottomRight_Black"));
			break;

		case NOBORDER:
			m_SpriteTile->PlayAnimation(_T("NoBorder_Black"));
			break;

		case NORMAL:
			m_SpriteTile->PlayAnimation(_T("Normal_Black"));
			break;
		}

		break;
	}


}

void BaseTile::Update( const star::Context& context )
{

	m_ElpasedTime += static_cast<float>(context.mTimeManager->GetSeconds());
	
	if(m_ElpasedTime >= m_SwitchTime)
	{
		m_ElpasedTime=0;
		switch (m_TileColor)
		{
		case BLACKTILE:
			SetColor(TileColor::WHITETILE);
			break;
		case WHITETILE:
			SetColor(TileColor::BLACKTILE);
			break;
		}
	}
}

void BaseTile::AfterInitialzed()
{
	SetTypeColor(m_Type,m_TileColor);
}
